<?php
namespace Home\Controller;

// use Think\Controller;

class ScoreController extends HomeController 
{
    public function index()
    {
        if(empty($_SESSION['home_user']['phone'])) {

            
            $movie = M('movie');
            
            //积分兑换商品模块
            $count = M()->table('lamp_movie  as  m')->field('m.id,m.showdate,m.name as mname,i.icon,c.name as cname')->join('lamp_img  as  i  on  m.id = i.mid and face=1')->join('lamp_category as c on m.cid=c.id')->limit(5)->order('m.id desc')->count();
            // var_dump($count);exit;
            // $Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            // $show = $Page->show();// 分页显示输出

             $data = M()->table('lamp_movie  as  m')->field('m.id,m.showdate,m.name as mname,i.icon,c.name as cname')->join('lamp_img  as  i  on  m.id = i.mid and face=1')->join('lamp_category as c on m.cid=c.id')->limit(5)->order('m.id desc')->select();
            // var_dump($data);exit;
            // $this->assign('page',$show);
            $this->assign('search',$data);
            $this->display();
            exit;
        }


        // $this->display();

        //右侧显示积分模块
        $score = M('score');
        // var_dump($_SESSION);exit;
        $list = $score->where('uid='.$_SESSION['home_user']['id'])->find();


        //登录显示积分兑换商品
        $movie = M('movie');
        
        //积分兑换商品模块
        $count = M()->table('lamp_movie  as  m')->field('m.id,m.showdate,m.name as mname,i.icon,c.name as cname')->join('lamp_img  as  i  on  m.id = i.mid and face=1')->join('lamp_category as c on m.cid=c.id')->limit(5)->order('m.id desc')->count();
        // var_dump($count);exit;
        // $Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        // $show = $Page->show();// 分页显示输出

         $data = M()->table('lamp_movie  as  m')->field('m.id,m.showdate,m.name as mname,i.icon,c.name as cname')->join('lamp_img  as  i  on  m.id = i.mid and face=1')->join('lamp_category as c on m.cid=c.id')->limit(5)->order('m.id desc')->select();
        // var_dump($data);exit;
        // $this->assign('page',$show);
        $this->assign('search',$data);

        //分配变量
        $this->assign('list',$list['score']);
        // 加载模板
        $this->display();
    }
    public function doadd()
    {	
    	$score = M('score');
    	

    	if(empty($_SESSION['home_user']['phone'])) {
    		$this->error('请登录!!!');
    		exit;
    	}
        //判断用户积分的状态
        $status = $score->where('uid='.$_SESSION['home_user']['id'])->field('status')->find();
        if($status['status']==1) {
            $this->error('你的账户已冻结~');
            exit;
        }

        $list = $score->where('uid='.$_SESSION['home_user']['id'])->find();

        if(!$list) {
            $data['time']=time();
            $data['uid'] = $_SESSION['home_user']['id'];
            $data = $score->add($data);
            $score->where('uid='.$_SESSION['home_user']['id'])->setInc('score',50);
            $this->success('首次签到赠送50积分',U('score/index'));

        } else {

                    //判断时间是否超过24小时
                    $time = $score->where('uid='.$_SESSION['home_user']['id'])->field('time')->find();
                    $time = $time['time'];
                    $nowtime = time();
                    // echo($time);
                    // var_dump($nowtime);exit;


                    if($nowtime - $time >= 60*60*24) {
                        //执行积分增加的操作

                        $score->where('uid='.$_SESSION['home_user']['id'])->setInc('score',5);

                        $data['time'] = $nowtime;
                        $data['uid'] = $_SESSION['home_user']['id'];

                        // $score->time = time();
                        $d = $score->where('uid='.$_SESSION['home_user']['id'])->save($data);

                        if($d >= 0){
                                $this->success("积分赠送成功！",U('Score/index'));
                            }else{
                                $this->error("积分赠送失败");
                            }

                    } else {
                        $this->error('24小时内不许重复签到哦！！！');
                    }
        }


    }







}